﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using T.FAS.Runtime.MF.Entity.ElsaEntity;
using T.ServiceComponent.DAC.Service;

namespace T.FAS.Runtime.MF.Dao.Dao
{
    public class WorkflowExecutionLogRecordDao : DaoService<WorkflowExecutionLogRecord>
    {
        public override string TableName => "WorkflowExecutionLogRecords";
        public override string IDColumnName => "Id";
        public override string DBName => "TFASMF";

        public List<WorkflowExecutionLogRecord> GetExecuteRecordByInstanceId(string instanceId)
        {
            var records = new List<WorkflowExecutionLogRecord>();
            string sql = @$"SELECT ""Id"", ""WorkflowDefinitionId"", ""WorkflowDefinitionVersionId"", 
                        ""WorkflowInstanceId"", ""WorkflowVersion"", ""ActivityInstanceId"", ""ParentActivityInstanceId"", 
                        ""ActivityId"", ""ActivityType"", ""ActivityTypeVersion"", ""ActivityName"", ""ActivityNodeId"", 
                        ""Timestamp"", ""Sequence"", ""EventName"", ""Message"", ""Source"", ""SerializedActivityState"",
                        ""SerializedPayload"", ""TenantId""
                        FROM ""Elsa"".""WorkflowExecutionLogRecords"" 
                        Where ""WorkflowInstanceId""='{instanceId}'";

            var dataSet = this.DB.ExecuteDataSet(sql);

            if (dataSet != null && dataSet.Tables.Count > 0)
            {
                foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                {
                    var record = ConvertToObject(base.Columns, dataRow);
                    if (record.Timestamp.HasValue)
                    {
                        record.Timestamp = record.Timestamp.Value.AddHours(8);
                    }
                    records.Add(record);
                }
            }
            return records;
        }
        public override WorkflowExecutionLogRecord ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);

            var data = base.ConvertToObject(myCol, row);

            return data;
        }

    }
}

